package com.csi.dao.impl;

import com.csi.dao.UserInfoDao;
import com.csi.domain.UserInfo;
import com.csi.utils.JDBCUtils;
import com.csi.utils.MD5Utils;

import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserInfoDaoImpl extends JDBCUtils implements UserInfoDao {

    @Override
    public UserInfo login(String phone , String password) throws SQLException {
        final String SQL="SELECT * FROM hotel_user WHERE userPhone=? AND userPassword=?";
        Connection connection = this.getConnection ();
        PreparedStatement ps = connection.prepareStatement ( SQL );
        ps.setString ( 1,phone );
        ps.setString ( 2,password );
        ResultSet rs = ps.executeQuery ();
        UserInfo userInfo = null;
        if (rs.next ()){
            userInfo = new UserInfo ();
            userInfo.setUserId ( rs.getInt ( "userId" ) );
            userInfo.setUserName ( rs.getString ( "userName" ) );
            userInfo.setUserHead ( rs.getString ( "userHead" ) );
            userInfo.setPhone ( rs.getString ( "userPhone" ) );
            userInfo.setEmail ( rs.getString ( "userEmail" ) );
            userInfo.setSex ( rs.getString ( "userSex" ) );
        }
        release ( rs,ps,connection);
        return userInfo;
    }

    @Override
    public int register(UserInfo userInfo) throws SQLException {
        final String SQL = "INSERT INTO hotel_user(userName,userPassword,userPhone,userEmail,userSex) VALUE(?,?,?,?,?)";
        Connection connection = this.getConnection ();
        PreparedStatement ps = connection.prepareStatement ( SQL );
        MD5Utils md5Utils = new MD5Utils ();
        String password = null;
        try {
            password = md5Utils.toMD5 ( userInfo.getPassword () );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace ();
        }
        ps.setString ( 1,userInfo.getUserName () );
        ps.setString ( 2,password );
        ps.setString ( 3,userInfo.getPhone () );
        ps.setString ( 4,userInfo.getEmail () );
        ps.setString ( 5,userInfo.getSex () );
        int num = ps.executeUpdate ();
        release ( ps,connection );
        return num;
    }

    @Override
    public int update(UserInfo userInfo) throws SQLException {
        final String SQL="UPDATE hotel_user SET userName=?,userHead=?,userPhone=?,userSex=? WHERE userId=?";
        Connection connection = this.getConnection ();
        PreparedStatement ps = connection.prepareStatement ( SQL );
        ps.setString ( 1,userInfo.getUserName () );
        ps.setString ( 2, userInfo.getUserHead () );
        ps.setString ( 3, userInfo.getPhone () );
        ps.setString ( 4, userInfo.getSex () );
        ps.setInt ( 5,userInfo.getUserId () );
        int num = ps.executeUpdate ();
        release ( ps,connection );
        return num;
    }

    @Override
    public int update(String password,int userId) throws SQLException {
        final String SQL="UPDATE hotel_user SET userPassword=? WHERE userId=?";
        Connection connection = this.getConnection ();
        PreparedStatement ps = connection.prepareStatement ( SQL );
        ps.setString ( 1,password );
        ps.setInt ( 2,userId );
        int num = ps.executeUpdate ();
        release ( ps,connection );
        return num;
    }
}
